package com.crm.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.crm.entity.Netaskers;
import com.crm.entity.Role;
import com.crm.repository.NetaskersRepository;
import com.crm.service.NetaskersService;
import com.crm.util.FenyeUtil;
@Service
public class NetaskersServiceImpl implements NetaskersService {
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Autowired
	private NetaskersRepository repository;
	@Override
	public FenyeUtil<Netaskers> getNetAskerByFenYe(Netaskers n, Integer page, Integer limit) {
		Pageable pageable=PageRequest.of(page-1, limit); 
		 Page<Netaskers> nlist = repository.findAll(get(n), pageable);
		FenyeUtil<Netaskers> f=new FenyeUtil<Netaskers>();
		f.setPage(page);//把page传入
		f.setLimit(limit);
		f.setData(nlist.getContent());
		f.setCount((int)nlist.getTotalElements());
		return f;
	}
public Specification<Netaskers> get(Netaskers n){
		
		return new Specification<Netaskers>() {
			
			

			@Override
			public Predicate toPredicate(Root<Netaskers> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate predicate=criteriaBuilder.conjunction();
				List<Expression<Boolean>> expressions=predicate.getExpressions();
				if(!StringUtils.isEmpty(n.getNetAskerName())) {
					expressions.add(criteriaBuilder.like(root.get("us").get("userName"), "%"+n.getNetAskerName()+"%"));
				}
				return predicate;
			}
		};
		
	}
	@Override
	public String addNetaskers(Netaskers n) {
		try {
			
			repository.saveAndFlush(n);
			
			return "添加成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "添加失败";
		}
	}

	@Override
	public String updateNetaskers(Netaskers n) {
          try {
			
			repository.saveAndFlush(n);
			
			return "修改成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "修改失败";
		}
	}

	@Override
	public String deleteNetaskers(Integer id) {
        try {
			
			repository.deleteById(id);
			
			return "删除成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "删除失败";
		}
	}
	@Override
	public Integer StuCount(Integer netAskerId) {
		// TODO Auto-generated method stub
		return repository.StuCount(netAskerId);
	}
	@Override
	public List<String> StuId(Integer netAskerId) {
		// TODO Auto-generated method stub
		return repository.StuId(netAskerId);
	}
	@Transactional
	@Override
	public Integer status(String status, Integer netAskerId) {
		// TODO Auto-generated method stub
		return repository.status(status, netAskerId);
	}
	@Override
	public Integer netId(String userId) {
		// TODO Auto-generated method stub
		return repository.netId(userId);
	}

	
}
